bucket_options = {
    480: [(416, 960),
          (448, 864),
          (480, 832),
          (512, 768),
          (544, 704),
          (576, 672),
          (608, 640),
          (640, 608),
          (672, 576),
          (704, 544),
          (768, 512),
          (832, 480),
          (864, 448),
          (960, 416)],
    720: [(624, 1456),
          (672, 1312),
          (720, 1280),
          (768, 1152),
          (816, 1056),
          (864, 1008),
          (912, 960),
          (960, 912),
          (1008, 864),
          (1056, 816),
          (1152, 768),
          (1280, 720),
          (1312, 672),
          (1456, 624)],
    540: [(464, 1088),
          (496, 976),
          (544, 960),
          (576, 864),
          (608, 800),
          (656, 752),
          (688, 720),
          (720, 688),
          (752, 656),
          (800, 608),
          (864, 576),
          (960, 544),
          (976, 496),
          (1088, 464)],
    360: [(304, 720),
          (336, 656),
          (352, 640),
          (384, 576),
          (400, 528),
          (432, 496),
          (448, 480),
          (480, 448),
          (496, 432),
          (528, 400),
          (576, 384),
          (640, 352),
          (656, 336),
          (720, 304)],
}

def find_nearest_bucket(h, w, resolution=640):
    min_metric = float('inf')
    best_bucket = None
    for (bucket_h, bucket_w) in bucket_options[resolution]:
        metric = abs(h * bucket_w - w * bucket_h)
        if metric <= min_metric:
            min_metric = metric
            best_bucket = (bucket_h, bucket_w)
    return best_bucket

